package com.classichu.jetpacksimple.data;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;

import com.classichu.jetpacksimple.data.local.AppDatabaseManager;
import com.classichu.jetpacksimple.data.local.entity.ZhihuStory;
import com.classichu.jetpacksimple.data.remote.ApiManager;
import com.classichu.jetpacksimple.data.remote.model.ZhihuModel;
import com.classichu.jetpacksimple.helper.ExecutorServiceHelper;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DataRepository {
    //===============
    private DataRepository() {
        mAppDatabaseManager = AppDatabaseManager.getInstance();
        mApiManager = ApiManager.getInstance();
    }

    public static DataRepository getInstance() {
        return Inner.sInstance;
    }

    private static class Inner {
        private static DataRepository sInstance = new DataRepository();
    }
    //===============

    //单例
    //关联/包装 获取数据
  /*  private IDateSouce mRemoteDataSource;

    private IDateSouce mLocalDataSource;

    public DataRepository(IDateSouce mRemoteDataSource, IDateSouce mLocalDataSource) {
        this.mRemoteDataSource = mRemoteDataSource;
        this.mLocalDataSource = mLocalDataSource;
    }*/

    /*public void loading()
    {
        mLocalDataSource.refreshList();
    }*/
/*    public DataRepository() {
        mRemoteDataSource = null;
        mLocalDataSource = null;
    }
*/

    AppDatabaseManager mAppDatabaseManager;
    ApiManager mApiManager;
    private final MutableLiveData<List<ZhihuStory>> mZhihuStoryListLiveData=new MutableLiveData<>();
    //    public LiveData<Resource<List<ZhihuModel>>> getLatestZhihuList() {
    public LiveData<List<ZhihuStory>> getLatestZhihuStoryList() {
        mApiManager.getZhihuApi().getLatestZhihuModel()
                .enqueue(new Callback<ZhihuModel>() {
                    @Override
                    public void onResponse(Call<ZhihuModel> call, Response<ZhihuModel> response) {
                        if (response.isSuccessful()) {
                            ZhihuModel zhihuModel = response.body();
                            List<ZhihuStory> zhihuStoryList = zhihuModel.getStories();
                            //update DATE
                            mZhihuDateTemp = zhihuModel.getDate();
                            //update
                            mZhihuStoryListLiveData.setValue(zhihuStoryList);
                            ExecutorServiceHelper.execute(new Runnable() {
                                @Override
                                public void run() {
                                    //saveDB
                                    //一般 Model 包含 Entity
//                            List<ZhihuStory> zhihuStoryList = DataTypeCovertHelper.model2Entity(zhihuModelList);
                                    mAppDatabaseManager.getAppDatabase().zhihuDao().insertList(zhihuStoryList);
                                }
                            });

                        }
                    }

                    @Override
                    public void onFailure(Call<ZhihuModel> call, Throwable t) {
                    }
                });
        return mZhihuStoryListLiveData;
    }

    private String mZhihuDateTemp = "";
    private MutableLiveData<Boolean> mIsLoading = new MutableLiveData<>();

    public LiveData<Boolean> IsLoading() {
        return mIsLoading;
    }

    public LiveData<List<ZhihuStory>> getBeforeZhihuStoryList() {
        mIsLoading.setValue(true);
        //
        mApiManager.getZhihuApi().getBeforeZhihuModel(mZhihuDateTemp)
                .enqueue(new Callback<ZhihuModel>() {
                    @Override
                    public void onResponse(Call<ZhihuModel> call, Response<ZhihuModel> response) {
                        if (response.isSuccessful()) {
                            ZhihuModel zhihuModel = response.body();
                            List<ZhihuStory> zhihuStoryList = zhihuModel.getStories();
                            //update DATE
                            mZhihuDateTemp = zhihuModel.getDate();
                            //update
                            mZhihuStoryListLiveData.setValue(zhihuStoryList);
                            ExecutorServiceHelper.execute(new Runnable() {
                                @Override
                                public void run() {
                                    //saveDB
                                    //一般 Model 包含 Entity
//                            List<ZhihuStory> zhihuStoryList = DataTypeCovertHelper.model2Entity(zhihuModelList);
                                    mAppDatabaseManager.getAppDatabase().zhihuDao().insertList(zhihuStoryList);
                                }
                            });
                            //
                            mIsLoading.setValue(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<ZhihuModel> call, Throwable t) {
                        mIsLoading.setValue(false);
                    }
                });
        return mZhihuStoryListLiveData;
    }
}
